//
//  ViewController.swift
//  ATSectionWaterfallFlowLayoutExample
//
//  Created by dfsx6 on 2021/9/2.
//

import UIKit
import ATSectionWaterfallFlowLayout

//MARK:-
class ViewController: UIViewController {

    fileprivate let cellId = "TitleCollectionViewCellID"
    fileprivate let reuseViewId = "TitleCollectionReusableViewID"
    fileprivate let decorationId = "TitleCollectionDecorationViewID"
    fileprivate let coverId = "CoverCollectionDecorationViewID"

    fileprivate lazy var collectionView: UICollectionView = self.preferredCollectionView()
    fileprivate var dataArr: [String] = []

}

//MARK:-
extension ViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        for _ in 0..<2 {
            //dataArr.append("自定义瀑布流")
            dataArr.append("自定义瀑布流 UICollectionViewFlowLayout自定义瀑布流 UICollectionViewFlowLayout自定义瀑布流 UICollectionViewFlowLayout自定义瀑布流 UICollectionViewFlowLayout自定义瀑布流 UICollectionViewFlowLayout自定义瀑布流 UICollectionViewFlowLayout")
        }
        
        view.addSubview(collectionView)
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        collectionView.frame = view.bounds
    }

}

//MARK:-
extension ViewController {
    
    ///
    fileprivate func preferredCollectionView() -> UICollectionView {
        let layout = ATSectionWaterfallFlowLayout.init()
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
//        layout.headerReferenceSize = CGSize.init(width: view.frame.width, height: 50)
//        layout.footerReferenceSize = CGSize.init(width: view.frame.width, height: 50)
        layout.register(UINib.init(nibName: "TitleCollectionDecorationView", bundle: nil), forDecorationViewOfKind: decorationId)
        layout.register(UINib.init(nibName: "CoverCollectionDecorationView", bundle: nil), forDecorationViewOfKind: coverId)

        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(UINib.init(nibName: "TitleCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellId)
        collectionView.register(UINib.init(nibName: "TitleCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseViewId)
        collectionView.register(UINib.init(nibName: "TitleCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reuseViewId)



        return collectionView
    }
}


//MARK:-
extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TitleCollectionViewCell else {
            return UICollectionViewCell.init()
        }
        
        cell.titleLabel.text = "\(dataArr[indexPath.row]) \(indexPath.row)"
        if indexPath.section == 0 {
            cell.contentWidth.constant = view.frame.width - 20
        } else {
            cell.contentWidth.constant = (view.frame.width - 30)/2
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        guard let reuseView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reuseViewId, for: indexPath) as? TitleCollectionReusableView else {
            return UICollectionReusableView.init()
        }
        
        if kind == UICollectionView.elementKindSectionHeader {
            reuseView.titleLabel.text = "this is header \(indexPath.section)-\(indexPath.row)"
        } else if kind == UICollectionView.elementKindSectionFooter {
            reuseView.titleLabel.text = "this is footer \(indexPath.section)-\(indexPath.row)"
        }
        
        //reuseView.backgroundColor = .clear
        return reuseView
    }

}

//MARK:-
extension ViewController: UICollectionViewDelegate {
    
}

//MARK:-
extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: view.frame.width, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: view.frame.width, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
    }
}

//MARK:-
extension ViewController: ATSectionWaterfallDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, layoutAttributesForDecorationViewWith indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        switch indexPath.section {
        case 0:
            return UICollectionViewLayoutAttributes.init(forDecorationViewOfKind: decorationId, with: indexPath)
        default:
            return nil
        }
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, numberOfColumnsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return 2
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, itemAlignmentAt section: Int) -> ATSectionWaterfallFlowLayout.ItemAlignment? {
        switch section {
        case 0:
            return .top
        default:
            return .waterfall
        }
    }


}

